﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Threading;
using System.Reflection; // 引用这个才能使用Missing字段
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.IO;
using CCWin;
namespace weiqijiance
{
    public partial class TESTWE : Form
    {
        public 控制流程.flowcrtl flowbass = new 控制流程.flowcrtl();
        public string sshebei = "轴重";
        public mainform m_mainform;
        public string sdetid;
        /// <summary>
        /// 结果变量
        /// </summary>
        /// 
        public bool jcxjs = false;//检测线结束标识
        public bool cdjc = false;//轮重制动重叠检测
        public bool cbjd = false;//仪表检定
        public float fJGlunzhongzuo;
        public float fJGlunzhongyou;

        public float flunzhongzuo;
        public float flunzhongyou;
       
        public Thread t1;
        public string xianshineirong;
        public string businessId;

        public float[,] fZero = new float[10, 10000];
        public int[] izbzlz = new int[6] { 0, 0, 0, 0, 0, 0 };


       
        public bool[] bhongwaiqf = new bool[16] { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false };//红外是否取反
        public int[] ihongwaidwsj = new int[16] { 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 };//红外定位时间单位毫秒
      /// <summary>
      /// 通道参数 包括检测时间，模拟信号，红外信号及控制信号等
      /// </summary>
        public float fjcsj;//检测时间
        public int xssj = 0;//检测时间
        public int idwsj;//红外定位时间

        public float[] fAIDataZero = new float[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };//零点漂移值
        public int ilunzhongtdz;
        public int ilunzhongtdy;

        public int ilunzhongHWQ=-1;
        public int ilunzhongHWH=-1;
        public int izhoushu;
      
        public bool sdjcs;
        public string xianshijieguo;
        bool isVideoOff;

        public int iwaiting;

        public bool bpicoff = false;

        // /声明INI文件的写操作函数 WritePrivateProfileString()
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //声明INI文件的读操作函数 GetPrivateProfileString()
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        private string sPath = null;


        public TESTWE(int Width, int Height)
        {
            InitializeComponent();
            panel_Width = Width;
            panel_Height = Height;
            gettongdao();
            xinxixianshi.Text = "轮重检测";

            x = this.Width;
            y = this.Height;
            setTag(this);

        }
        bool bzjddzj = false;
        bool bbhzbzl = false;
        bool bfbxs = false;
        bool bweighty = false;
        bool bzbzlpd = false;
        public void init()
        {
            iniclass objini = new iniclass();
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\安综检仪表配置.ini";
            string path1 = string.Format(@"{0}", path);
            objini.Ini(path1);
            jcxjs = (objini.ReadValue("轮重", "检测线结束设备").Trim() == "1") ? true : false;
            cdjc = (objini.ReadValue("制动", "轴重制动重叠检测").Trim() == "1") ? true : false;
            bbhzbzl = (objini.ReadValue("轮重", "包含整备质量").Trim() == "1") ? true : false;
            bzjddzj = objini.ReadValue("轮重", "只检单独综检").Trim().Contains("是") ? true : false;
            isVideoOff = objini.ReadValue("轮重", "轴重不录像").Contains("是") ? true : false;
            float.TryParse(objini.ReadValue("轮重", "检测时间"), out fjcsj);
            bfbxs = objini.ReadValue("轮重", "结果采用比例显示") == "是" ? true : false;
            if (fjcsj < 100)
            {
                fjcsj = 3000;
            }
            int.TryParse(objini.ReadValue("轮重", "信息显示时间"), out xssj);
            if (xssj < 100)
            {
                xssj = 1000;
            }
            int.TryParse(objini.ReadValue("轮重", "等待视频录制时间"), out iwaiting);
            if (iwaiting < 100)
            {
                iwaiting = 1000;
            }
            bpicoff = objini.ReadValue("轮重", "轴重不抓拍").Contains("是") ? true : false;
            bweighty = objini.ReadValue("轮重", "摩托车使用右轮重").Contains("是") ? true : false;
            bzbzlpd = objini.ReadValue("轮重", "空车质量不进行评判").Contains("是") ? true : false;
        }
        public void start()
        {
            fJGlunzhongzuo = 0;
            fJGlunzhongyou = 0;
            bianliangchushihua();

            t1 = new Thread(new ThreadStart(Thread1));
            t1.Priority = ThreadPriority.BelowNormal;
            t1.Start();
        }

        /// <summary>
        /// 变量初始化
        /// </summary>
        public void bianliangchushihua()
        {
            flunzhongzuo = 0;
            flunzhongyou = 0;
            
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\车辆信息.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);

        }
        #region 控件大小随窗体大小等比例缩放
        private float x;//定义当前窗体的宽度
        private float y;//定义当前窗体的高度
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            if (newy <= 0)
            {
                newy = 1;
            }
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
            {
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        #endregion

        //ini文件
        public void Ini(string path)
        {
            this.sPath = path;
        }
        public void Writue(string section, string key, string value)
        {
            // section=配置节，key=键名，value=键值，path=路径
            WritePrivateProfileString(section, key, value, sPath);
        }
        public string ReadValue(string section, string key)
        {
            // 每次从ini中读取多少字节
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);

            // section=配置节，key=键名，temp=上面，path=路径
            GetPrivateProfileString(section, key, "", temp, 255, sPath);
            return temp.ToString();
        }
        //ini文件


        private delegate void FlushClient();//代理
        public void xianshi()
        {
            if (this.xinxixianshi.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshi);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.xinxixianshi.Text = string.Format(@"{0}", xianshineirong);
            }
        }

        private void xianshizuo()
        {
            if (this.gauge_weight_left.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshizuo);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.gauge_weight_left.Visible = true;
                this.digitalGauge1.DigitCount = flunzhongzuo.ToString("0").Length < 3 ? 3 : flunzhongzuo.ToString("0.0").Length;
                this.digitalGauge1.Text = flunzhongzuo.ToString("0");
            }
        }

        private void xianshiyou()
        {
            if (this.gauge_weight_right.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshiyou);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.gauge_weight_right.Visible = true;
                this.digitalGauge2.DigitCount = flunzhongyou.ToString("0").Length < 3 ? 3 : flunzhongyou.ToString("0.0").Length;
                this.digitalGauge2.Text = flunzhongyou.ToString("0");
            }
        }
        protected void thisclose()
        {

            this.Invoke(new Action(this.Close));  //使用委托关闭该窗体
        }
        public void gettongdao()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\串口及通道设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);
            for (int i = 0; i < 10; i++)
            {
                string sgetname = ReadValue("通道设置", string.Format(@"模拟信号{0}", i + 1));
                if (sgetname=="轮重左")
                {
                    ilunzhongtdz = i;
                }
                if (sgetname=="轮重右")
                {
                    ilunzhongtdy = i;
                }
              
            }

            for (int i = 0; i < 4; i++)
            {
               
            }

            for (int i = 0; i < 16; i++)
            {
                string shongwai=ReadValue("通道设置", string.Format(@"红外{0}", i + 1));
                if (shongwai.Contains("轮重到位前") || shongwai.Contains("摩托车遥控"))
                {
                    ilunzhongHWQ = i;
                }
                if (shongwai.Contains("轮重到位后"))
                {
                    ilunzhongHWH = i;
                }
                shongwai = ReadValue("通道设置", string.Format(@"红外定位时间{0}", i + 1));
                int.TryParse(shongwai, out ihongwaidwsj[i]);
                 

                shongwai = ReadValue("通道设置", string.Format(@"红外是否取反{0}", i + 1));
                if (shongwai.Contains("是"))
                {
                    bhongwaiqf[i] = true;
                }
                else
                {
                    bhongwaiqf[i] = false;
                }
            }
        }


        public void getlingdian()//获取零点
        {
            for (int i = 0; i < 10; i++)
            {
                fAIDataZero[i] = m_mainform.DAC.fAIData[i];
            }

        }
        public void getdata()//处理信号板采集数值
        {
 
        }
        public void StratTest()
        {
            if (!flowbass.bzj && bzjddzj)
                return;

            try
            {
                if (flowbass.iVax < 1)
                {
                    flowbass.iVax = 1;
                    flowbass.bJCXCZDZ[0] = true;
                }
                ///获取零点位置
                for (int i = 0; i < flowbass.iVax; i++)
                {
                    DetectWE(i);
                    if (flowbass.IsCancle())
                    {
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                if (!(ex is System.Threading.ThreadAbortException))
                {
                    flowbass.LedWrite(flowbass.sPlate, "异常,停止检测", 1);
                    m_mainform.SaveErrorLog("轴重检测流程", ex.Message.ToString());//problem
                }
            }

        }

        /// <summary>
        /// 红外信号判断是否到位 路数 是否取反 保持稳定时间
        /// </summary>
        /// <param name="sName"></param>
        public void WaitEvent(int lushu, bool bqufan, int stime)
        {
            DateTime dstart = DateTime.Now;
            bool bzt = false;
            bool firstzt = false;
            while (true)
            {
                if (cdjc&& !flowbass.bJCZBZL)
                    flowbass.izhuangtai = m_mainform.flowbr.flowbass.izhuangtai;
                if (flowbass.IsCancle())
                {
                    
                    flowbass.LedWrite("检测取消", "", 1);
                    return;
                }
                bool bzhuangtai = m_mainform.DAC.bDIData[lushu];
                if (bqufan)
                {
                    bzhuangtai = !bzhuangtai;
                }
                if (bzhuangtai)
                {
                    firstzt = true;
                    if (!bzt)
                    {
                        //flowbass.LedWrite(flowbass.sPlate, "请驶上制动台", 1);
                        dstart = DateTime.Now;
                    }
                    else
                    {
                        if (DateTime.Now > dstart.AddMilliseconds(stime))
                        {
                            break;
                        }
                        flowbass.LedWrite(flowbass.sPlate, "停车", 1);
                    }
                }
                else
                {
                
                    dstart = DateTime.Now;
                    bzt = true;
                    if (firstzt)
                    {
                        flowbass.LedWrite(flowbass.sPlate, "未到位", 1);
                    }
                   
                }
                Thread.Sleep(100);
            }
        }
        public void DetectWE(int ivax)
        {
            string sjcdlzgw = "";
            if (flowbass.gwjysbbh.Contains(","))
            {
                string[] sbbhs = flowbass.gwjysbbh.Split(',');
                if (sbbhs.Length >= ivax + 1)
                {
                    sjcdlzgw = sbbhs[ivax];
                }
                else
                {
                    sjcdlzgw = sbbhs[0];
                }
            }
            else
            {
                sjcdlzgw = flowbass.gwjysbbh;
            }
            string[] swezpph = new string[5] { "A302", "A303", "A304", "A305", "A306" };
            string revert;
            flowbass.UpdateLoginRecord(DateTime.Now.ToString(), "轮重检测开始", flowbass.sDetectID);
            if (flowbass.bJCZBZL && bbhzbzl&&flowbass.iVax < 3)
            {
                flowbass.bJCXCZDZ[0] = true;
                flowbass.bJCXCZDZ[1] = true;
            }
            else
            {
                if (flowbass.bweight)
                {
                    if (!flowbass.bJCXCZDZ[ivax])
                    {
                        flowbass.LedWrite(flowbass.sPlate, string.Format(@"第{0}轴免检", ivax + 1), 1);
                        return;
                    }
                }
                else
                {
                    flowbass.LedWrite(flowbass.sPlate, string.Format(@"第{0}轴免检", ivax + 1), 1);
                    return;
                }
            }

            if (flowbass.bJCZBZL && bbhzbzl && ivax == 0 && flowbass.iVax < 3)
            {
                if (flowbass.bnj)
                {
                    flowbass.objcarclass.tongdao1 = flowbass.izbzlqtongdao.ToString();
                    flowbass.objcarclass.tongdao2 = flowbass.izbzlhtongdao.ToString();
                    revert = flowbass.objajlwbass.SendJYXMKSSJ("Z1", "开始", flowbass.gwjysbbhzbzl, flowbass.izbzlhtongdao.ToString(), flowbass.objcarclass);
                    if (!revert.Contains("1"))
                    {
                        flowbass.LedWrite("联网失败", revert, 1); Thread.Sleep(2000);
                    }
                }
            }



            xianshineirong = string.Format(@"第{0}轴驶上轮重台", ivax + 1);
            xianshi();
            flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);

            // 红外定位
            if (ilunzhongHWQ > -1)//配置红外后才判断红外定位
            {
                WaitEvent(ilunzhongHWQ, bhongwaiqf[ilunzhongHWQ], ihongwaidwsj[ilunzhongHWQ]);
            }

            if (flowbass.sjcbz.Contains("38900"))
            {
                if (!isVideoOff)
                {
                    flowbass.objcarclass.tongdao1 = flowbass.ilzgwtongdao.ToString();
                    revert = flowbass.objajlwbass.SendJYXMKSSJ("WE" + (ivax + 1).ToString(), "开始", sjcdlzgw, flowbass.ilzgwtongdao.ToString(), flowbass.objcarclass);
                    if (!revert.Contains("1"))
                    {
                        flowbass.LedWrite("联网失败", revert, 1); Thread.Sleep(2000);
                    }
                }

            }
            xianshineirong = string.Format(@"轮重检测开始...", ivax + 1);
            xianshi();
            flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
 


            if (cdjc && !bbhzbzl)//flowbass.bJCZBZL///单独重叠检测
            {
                flowbass.izhuangtai = m_mainform.flowbr.flowbass.izhuangtai;
            }
            else if (cdjc && bbhzbzl && !flowbass.bJCZBZL && flowbass.iVax < 3)//重叠检测，还包含整备质量，没有整备质量
            {
                flowbass.izhuangtai = m_mainform.flowbr.flowbass.izhuangtai;
            }
            else
            {
                //flowbass.izhuangtai = m_mainform.flowbr.flowbass.izhuangtai;
            }
            //if (cdjc && !flowbass.bJCZBZL)
            //    flowbass.izhuangtai = m_mainform.flowbr.flowbass.izhuangtai;
            if (flowbass.IsCancle())
            {
                flowbass.LedWrite("检测取消", "", 1);
                return;
            }
            //测量数据
            DateTime dstart = DateTime.Now;
            DateTime dstartCYZ = DateTime.Now;
            int icyz = 0;
            float flunzhong1 = 0;
            float flunzhong2 = 0;

            var flunzhongzuo_date = 0.0f;
            var flunzhongyou_date = 0.0f;
           while(true)
            {
                Thread.Sleep(10);
                if (cdjc && !bbhzbzl)//flowbass.bJCZBZL///单独重叠检测
                {
                    flowbass.izhuangtai = m_mainform.flowbr.flowbass.izhuangtai;
                }
                else if (cdjc && bbhzbzl && !flowbass.bJCZBZL && flowbass.iVax < 3)//重叠检测，还包含整备质量，没有整备质量
                {
                    flowbass.izhuangtai = m_mainform.flowbr.flowbass.izhuangtai;
                }
                else
                {
                }
                //if (cdjc && !flowbass.bJCZBZL)
                //    flowbass.izhuangtai = m_mainform.flowbr.flowbass.izhuangtai;
                if (flowbass.IsCancle())
                {
                    flowbass.LedWrite("检测取消", "", 1);
                    return;
                }
                if (m_mainform.DateDiff(DateTime.Now, dstart) > fjcsj)
                {
                    break;
                }

                dstartCYZ = DateTime.Now;
                icyz = 0;
                flunzhong1 = 0;
                flunzhong2 = 0;
                while(true)
                {
                    Thread.Sleep(10);
                    if (m_mainform.DateDiff(DateTime.Now, dstartCYZ) > 100)
                    {
                        break;
                    }
                    flunzhong1 = flunzhong1 + m_mainform.DAC.getjsz("轮重左", m_mainform.DAC.fAIData[ilunzhongtdz]) - m_mainform.DAC.getjsz("轮重左", m_mainform.DAC.fAIDataZero[ilunzhongtdz]);
                    flunzhong2 = flunzhong2 + m_mainform.DAC.getjsz("轮重右", m_mainform.DAC.fAIData[ilunzhongtdy]) - m_mainform.DAC.getjsz("轮重右", m_mainform.DAC.fAIDataZero[ilunzhongtdy]);
                    icyz++;
                    
                }   

                if (flowbass.sCllx.Contains("三轮") && ivax + 1 == 1)
                {
                    //flunzhongzuo = 0;
                    //if (flunzhongzuo < 1)
                    //{
                    //    flunzhongzuo = m_mainform.getRandom(1, 5) ;//lzj 测试数据
                    //}
                    //xianshiyou();
                    flunzhongzuo = (float)(flunzhong1 / icyz);
                    if (flunzhongzuo < 1)
                    {
                        flunzhongzuo = m_mainform.getRandom(1, 3);//lzj 测试数据
                    }
                    flunzhongzuo = (float)Math.Round(flunzhongzuo);
                    xianshizuo();
                    if (flunzhongzuo_date < flunzhongzuo)
                    {
                        flunzhongzuo_date = flunzhongzuo;
                    }
                    ///摩托车第一轴
                    if (bweighty)
                    {
                        flunzhongyou = (float)(flunzhong2 / icyz);
                        if (flunzhongyou < 1)
                        {
                            flunzhongyou = m_mainform.getRandom(1, 3);//lzj 测试数据
                        }
                        flunzhongyou = (float)Math.Round(flunzhongyou);
                        xianshiyou();
                        if (flunzhongyou_date < flunzhongyou)
                        {
                            flunzhongyou_date = flunzhongyou;
                        }
                    }


                }
                else
                {
                    flunzhongzuo = (float)(flunzhong1 / icyz);
                    if (flunzhongzuo < 1)
                    {
                        flunzhongzuo = m_mainform.getRandom(1, 3) ;//lzj 测试数据
                    }
                    flunzhongzuo = (float)Math.Round(flunzhongzuo);
                    xianshizuo();
                    flunzhongyou = (float)(flunzhong2 / icyz);
                    if (flunzhongyou < 1)
                    {
                        flunzhongyou = m_mainform.getRandom(1, 3) ;//lzj 测试数据
                    }
                    flunzhongyou = (float)Math.Round(flunzhongyou);
                    xianshiyou();

                    if (flunzhongzuo_date < flunzhongzuo)
                    {
                        flunzhongzuo_date = flunzhongzuo;
                    }

                    if (flunzhongyou_date < flunzhongyou)
                    {
                        flunzhongyou_date= flunzhongyou;
                    }
                }
            }
           flunzhongzuo = (int)flunzhongzuo_date;
           flunzhongyou = (int)flunzhongyou_date;
           xianshizuo();
           xianshiyou();
           if (flowbass.sCllx.Contains("摩托"))
           {
               if (flowbass.sCllx.Contains("三轮") && ivax == 1)
               {
                   xianshijieguo = string.Format(@"左：{0} 右：{1}", (int)flunzhongzuo, (int)flunzhongyou);
                   
                   //flowbass.LedWrite(flowbass.sPlate, "检测完成", 1);
                   //Thread.Sleep(xssj);
                   flowbass.LedWrite("轮重数据", string.Format(@"左:{0} 右:{1}", (int)flunzhongzuo, (int)flunzhongyou), 1);
                   Thread.Sleep(xssj);
               }
               else
               {
                   if (bweighty)
                   {
                       //使用右轴重数据
                       xianshijieguo = string.Format(@"轮重:{0}", (int)flunzhongyou);
                       //flowbass.LedWrite(flowbass.sPlate, "检测完成", 1);
                       //Thread.Sleep(xssj);
                       flowbass.LedWrite("轮重数据", string.Format(@"{0}", (int)flunzhongyou), 1);
                       Thread.Sleep(xssj);
                   }
                   else
                   {
                       xianshijieguo = string.Format(@"轮重:{0}", (int)flunzhongzuo);
                       //flowbass.LedWrite(flowbass.sPlate, "检测完成", 1);
                       //Thread.Sleep(xssj);
                       flowbass.LedWrite("轮重数据", string.Format(@"{0}", (int)flunzhongzuo), 1);
                       Thread.Sleep(xssj);
                   }

               }

           }
           else
           {
               xianshijieguo = string.Format(@"左:{0} 右:{1}", (int)flunzhongzuo, (int)flunzhongyou);
               //flowbass.LedWrite(flowbass.sPlate, "检测完成", 1);
               //Thread.Sleep(xssj);
               flowbass.LedWrite("轮重数据", string.Format(@"左:{0} 右:{1}", (int)flunzhongzuo, (int)flunzhongyou), 1);
               Thread.Sleep(xssj);
           }
           xianshineirong = xianshijieguo;
           xianshi();

           if (flowbass.bweight)
            {
                if (flowbass.sCllx.Contains("摩托"))
                {
                    if (!(flowbass.sCllx.Contains("三轮") && ivax == 1))
                    {
                        if (bweighty)
                        {
                            flunzhongzuo = 0;
                        }
                        else
                        {
                            flunzhongyou = 0;
                        }
                    }
                    flowbass.AddData_Check(flowbass.sDetectID, string.Format("DET_WEIGHTZ{0}", ivax + 1), "Detects_SJB", ((int)flunzhongzuo).ToString("0"));
                    flowbass.AddData_Check(flowbass.sDetectID, string.Format("DET_WEIGHTY{0}", ivax + 1), "Detects_SJB", ((int)flunzhongyou).ToString("0"));
                    flowbass.AddData_Check(flowbass.sDetectID, string.Format("DET_WEIGHTH{0}", ivax + 1), "Detects_SJB", ((int)flunzhongzuo + (int)flunzhongyou).ToString("0"));
                }
                else
                {
                    flowbass.AddData_Check(flowbass.sDetectID, string.Format("DET_WEIGHTZ{0}", ivax + 1), "Detects_SJB", ((int)flunzhongzuo).ToString("0"));
                    flowbass.AddData_Check(flowbass.sDetectID, string.Format("DET_WEIGHTY{0}", ivax + 1), "Detects_SJB", ((int)flunzhongyou).ToString("0"));
                    flowbass.AddData_Check(flowbass.sDetectID, string.Format("DET_WEIGHTH{0}", ivax + 1), "Detects_SJB", ((int)flunzhongzuo + (int)flunzhongyou).ToString("0"));
               
                }
                //非交叉检测最后一轴退出
            }

           int izbzlzz = 0;
           if (flowbass.iVax < 3)
           {
               if (flowbass.bJCZBZL && bbhzbzl)
               {
                   izbzlz[ivax] = (int)flunzhongzuo + (int)flunzhongyou;
                   if (ivax == 1)
                   {
                       for (int i = 0; i < 2; i++)
                       {
                           izbzlzz = izbzlzz + izbzlz[i];
                       }
                       string szbzlxz = "";
                       int iblsxz = 0;
                       int iblxxz = 0;
                       int iczsxz = 0;
                       int iczxxz = 0;
                       int izbzlsx = 0;
                       int izbzlxx = 0;
                       if (flowbass.sjcbz.Contains("38900"))
                       {
                           //izbzlzz = izbzlzz - m_mainform.iycyzl - flowbass.wbzl;
                           izbzlzz = izbzlzz - flowbass.m_iycyzl - flowbass.wbzl;
                           if (flowbass.sxcrh.Contains("注册") || flowbass.sxcrh.Contains("过户") || flowbass.sxcrh.Contains("新车"))//2019-801A过户使用注册登记限值
                           {
                               updatezbzlpanding(izbzlzz, ref  szbzlxz, ref  iblsxz, ref  iblxxz, ref  iczsxz, ref  iczxxz, ref  izbzlsx, ref  izbzlxx);
                               pandingzbzl(izbzlzz, szbzlxz, iblsxz, iblxxz, iczsxz, iczxxz, izbzlsx, izbzlxx, 0);
                           }
                           else
                           {
                               updatezbzlpanding_kczl(izbzlzz, ref  szbzlxz, ref  iblsxz, ref  iblxxz, ref  iczsxz, ref  iczxxz, ref  izbzlsx, ref  izbzlxx);
                               pandingzbzl(izbzlzz, szbzlxz, iblsxz, iblxxz, iczsxz, iczxxz, izbzlsx, izbzlxx, 1);
                           }
                       }
                       else
                       {
                           updatezbzlpanding(izbzlzz, ref  szbzlxz, ref  iblsxz, ref  iblxxz, ref  iczsxz, ref  iczxxz, ref  izbzlsx, ref  izbzlxx);
                           pandingzbzl(izbzlzz, szbzlxz, iblsxz, iblxxz, iczsxz, iczxxz, izbzlsx, izbzlxx, 0);
                       }
                       flowbass.AddData_Check(flowbass.sDetectID, "DET_ZBZL", "Detects_SJA", izbzlzz.ToString("0"));
                   }
               }
           }
           if (flowbass.sjcbz.Contains("38900"))
           {
               if (!isVideoOff)
               {
                  
                   flowbass.objcarclass.tongdao1 = flowbass.ilzgwtongdao.ToString();
                   revert = flowbass.objajlwbass.SendJYXMKSSJ("WE" + (ivax + 1).ToString(), "结束", sjcdlzgw, flowbass.ilzgwtongdao.ToString(), flowbass.objcarclass);
                   if (!(revert.Length > 0 && revert[0].ToString() == "1"))
                   {
                       if (flowbass.bnj)
                       {
                           flowbass.LedWrite("联网失败", revert, 1); Thread.Sleep(2000);
                       }
                   }
                   Thread.Sleep(iwaiting);//确保录像
               }

           }


           if (flowbass.bJCZBZL && bbhzbzl && ivax == 1 && flowbass.iVax < 3)
           {
               if (flowbass.bnj)
               {

                   flowbass.getProcessfinish(flowbass.sDetectID, "Z1");
                   flowbass.objcarclass.tongdao1 = flowbass.izbzlqtongdao.ToString();
                   flowbass.objcarclass.tongdao2 = flowbass.izbzlhtongdao.ToString();
                   revert = flowbass.objajlwbass.SendJYXMKSSJ("Z1", "结束", flowbass.gwjysbbhzbzl, flowbass.izbzlhtongdao.ToString(), flowbass.objcarclass);
                   if (!(revert.Length > 0 && revert[0].ToString() == "1"))
                   {
                       if (flowbass.bnj)
                       {
                           flowbass.LedWrite("联网失败", revert, 1); Thread.Sleep(2000);
                       }
                   }
                   Thread.Sleep(iwaiting);//确保录像
               }
           }
           flunzhongzuo = 0;
           flunzhongyou = 0;
           xianshizuo();
           xianshiyou();
        }
        public void Thread1()
        {
            try
            {
                StratTest();
                flowbass.UpdateLoginRecord(DateTime.Now.ToString(), "轮重检测结束", flowbass.sDetectID);
                if (!cdjc||bbhzbzl)
                {
                   
                    if (jcxjs)
                    {
                        xianshineirong = "检测线完成";
                        flowbass.UpdateLoginRecord(DateTime.Now.ToString(), "检测线完成", flowbass.sDetectID);
                        flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                        Thread.Sleep(xssj);
                    }

                    flowbass.LedWrite(flowbass.sgongwei, "", 1);
                    xianshineirong = flowbass.sgongwei;
                    xianshi();
                    end();
                    t1.Abort();
                    t1.DisableComObjectEagerCleanup();
                }
             }
            catch (Exception ex)
            {
                //错误日记 记录到当天日期下目录中
                m_mainform.baocunriji("轮重", ex.Message.ToString());
            }
            thisclose();
            
        }

        public int panel_Width;
        public int panel_Height;     

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                if (flowbass.izhuangtai == 2)
                {
                    flowbass.IsReadyForStart();

                    if (cdjc && !bbhzbzl)//flowbass.bJCZBZL///单独重叠检测
                    {
                        flowbass.izhuangtai = 0;
                        flowbass.sendend("E", flowbass.sDetectID);
                    }
                    else if (cdjc&& bbhzbzl && !flowbass.bJCZBZL)//重叠检测，还包含整备质量，没有整备质量
                    {
                        flowbass.izhuangtai = 0;
                        flowbass.sendend("E", flowbass.sDetectID);
                    }
                    else//有整备质量，正常流程
                    {
                        m_mainform.objyibiaobass.select(flowbass.sshebei);
                        m_mainform.bpanelshow = true;
                        m_mainform.showpanel();
                        flowbass.izhuangtai = 1;

                        //flowbass.IsReadyForStart();
                        bianliangchushihua();
                        t1 = new Thread(new ThreadStart(Thread1));
                        t1.Start();
                        //this.ShowDialog();
                    }
                }
                else
                {
                
                }
            }
            catch (Exception ex)
            {
                m_mainform.baocunriji("轴重打开窗体失败", ex.Message.ToString());
                m_mainform.SaveErrorLog("轴重打开窗体", ex.Message.ToString());//problem
            
            }
        }

        public void timerstart()
        {
            timer1.Enabled = true;
        }

        public void end()
        {           
            flowbass.izhuangtai = 0;
            flowbass.sendend("E", flowbass.sDetectID);
        }

        private void closeform()
        {
            if (this.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(closeform);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.Dispose();
                this.Close();
            }
        }

        private void 仪表设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            工位配置.WEsetting obj = new 工位配置.WEsetting();
            obj.ShowDialog();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            try{
                m_mainform.sethongwai(lblhongwai1, m_mainform.WaitEventZT(ilunzhongHWQ, bhongwaiqf[ilunzhongHWQ], ihongwaidwsj[ilunzhongHWQ]));
                m_mainform.sethongwai(toolStripLabel1, m_mainform.WaitEventZT(ilunzhongHWQ, bhongwaiqf[ilunzhongHWQ], ihongwaidwsj[ilunzhongHWQ]));
            }
            catch 
            { }
            try
            {
                if (cbjd)
                {
                    Thread.Sleep(10);
                    float flunzhong1 = 0;
                    float flunzhong2 = 0;
                    flunzhong1 = m_mainform.DAC.getjsz("轮重左", m_mainform.DAC.fAIData[ilunzhongtdz]) - m_mainform.DAC.getjsz("轮重左", m_mainform.DAC.fAIDataZero[ilunzhongtdz]);
                    flunzhong2 = m_mainform.DAC.getjsz("轮重右", m_mainform.DAC.fAIData[ilunzhongtdy]) - m_mainform.DAC.getjsz("轮重右", m_mainform.DAC.fAIDataZero[ilunzhongtdy]);

                    flunzhongzuo = flunzhong1;
                    xianshizuo();
                    flunzhongyou = flunzhong2;
                    xianshiyou();
                    flowbass.LedWrite("轮重", string.Format("左：{0} 右：{1}", flunzhongzuo.ToString("0"), flunzhongyou.ToString("0")), 1);
                }
            }
            catch
            { }
        }

        private void but_start_Click(object sender, EventArgs e)
        {
            t1 = new Thread(new ThreadStart(Thread1));
            t1.Priority = ThreadPriority.BelowNormal;
            t1.Start();
        }

        private void but_alone_Click(object sender, EventArgs e)
        {
            start();
        }

        private void But_close_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ShowControls(true);
            ShowButtons(true);
        }

        public void ShowControls(bool flag)
        {
            lblhongwai1.Visible = flag;
            label1.Visible = flag;
            label2.Visible = flag;
            gauge_weight_left.Visible = flag;
            gauge_weight_right.Visible = flag;

        }

        public void ShowButtons(bool flag)
        {
            but_alone.Visible = flag;
            but_tiaoling.Visible = flag;
        }

        private void 设备检定ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            xinxixianshi.Text = "正在检定";
            cbjd = true;
        }

        private void 取消检定ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            xinxixianshi.Text = "轴重检测";
            cbjd = false;
            flowbass.LedWrite(flowbass.sgongwei, "", 1);
        }

        private void but_tiaoling_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.tiaoling(1);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            start();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            m_mainform.DAC.tiaoling(1);
        }

        private void TESTWE_Shown(object sender, EventArgs e)
        {
            try
            {
                float newx = (this.Width) / x;
                float newy = (this.Height) / y;
                setControls(newx, newy, this);
                // ShowControls(false);
                ShowButtons(false);
                this.BackgroundImage = Image.FromFile(@"./ico/轮重背景.jpg");
                this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            }
            catch (Exception ex)
            {
                m_mainform.baocunriji("轴重Load失败", ex.Message.ToString());//problem
                m_mainform.SaveErrorLog("轴重Load失败", ex.Message.ToString());//problem
            }
        }

        public void updatezbzlpanding(int izbzljcz, ref string szbzlxz, ref int iblsxz, ref int iblxxz, ref int iczsxz, ref int iczxxz, ref int izbzlsx, ref int izbzlxx)
        {
            double izbzl = flowbass.iCLZBZL;
            string scllx = flowbass.sCllx;

            if (scllx.Contains("中型") || scllx.Contains("重型") || scllx.Contains("挂"))
            {
                szbzlxz = "±3％或±500kg";
                if (izbzl * 3 / 100 >= 500)
                {
                    izbzlsx = (int)Math.Round(izbzl * 3 / 100 + izbzl);
                    izbzlxx = (int)Math.Round(izbzl - izbzl * 3 / 100);
                    iblsxz = izbzlsx;
                    iblxxz = izbzlxx;
                }
                else
                {
                    izbzlsx = (int)Math.Round(500 + izbzl);
                    izbzlxx = (int)Math.Round(izbzl - 500);
                    iczsxz = izbzlsx;
                    iczxxz = izbzlxx;
                }

            }
            else if (scllx.Contains("低速"))
            {
                szbzlxz = "±5％或±100kg";

                if (izbzl * 5 / 100 >= 100)
                {
                    izbzlsx = (int)Math.Round(izbzl * 5 / 100 + izbzl);
                    izbzlxx = (int)Math.Round(izbzl - izbzl * 5 / 100);
                    iblsxz = izbzlsx;
                    iblxxz = izbzlxx;
                }
                else
                {
                    izbzlsx = (int)Math.Round(100 + izbzl);
                    izbzlxx = (int)Math.Round(izbzl - 100);
                    iczsxz = izbzlsx;
                    iczxxz = izbzlxx;
                }
            }
            else if (scllx.Contains("轻型") || scllx.Contains("微型"))
            {
                szbzlxz = "±3％或±100kg";

                if (izbzl * 3 / 100 >= 100)
                {
                    izbzlsx = (int)Math.Round(izbzl * 3 / 100 + izbzl);
                    izbzlxx = (int)Math.Round(izbzl - izbzl * 3 / 100);
                    iblsxz = izbzlsx;
                    iblxxz = izbzlxx;
                }
                else
                {
                    izbzlsx = (int)Math.Round(100 + izbzl);
                    izbzlxx = (int)Math.Round(izbzl - 100);
                    iczsxz = izbzlsx;
                    iczxxz = izbzlxx;
                }
            }
            else
            {
                szbzlxz = "±3％或±500kg";
                if (izbzl * 3 / 100 >= 500)
                {
                    izbzlsx = (int)Math.Round(izbzl * 3 / 100 + izbzl);
                    izbzlxx = (int)Math.Round(izbzl - izbzl * 3 / 100);
                    iblsxz = izbzlsx;
                    iblxxz = izbzlxx;
                }
                else
                {
                    izbzlsx = (int)Math.Round(500 + izbzl);
                    izbzlxx = (int)Math.Round(izbzl - 500);
                    iczsxz = izbzlsx;
                    iczxxz = izbzlxx;
                }
            }

            if (scllx.Contains("摩托"))
            {
                szbzlxz = "±10kg";
                izbzlsx = (int)Math.Round(10 + izbzl);
                izbzlxx = (int)Math.Round(izbzl - 10);
                iczsxz = izbzlsx;
                iczxxz = izbzlxx;
            }

        }

        public void updatezbzlpanding_kczl(int izbzljcz, ref string szbzlxz, ref int iblsxz, ref int iblxxz, ref int iczsxz, ref int iczxxz, ref int izbzlsx, ref int izbzlxx)
        {
            double izbzl = flowbass.iCLZBZL;
            string scllx = flowbass.sCllx;
            if (scllx.Contains("中型") || scllx.Contains("重型") || scllx.Contains("挂"))
            {
                szbzlxz = "±10％或±500kg";
                if (izbzl * 10 / 100 >= 500)
                {
                    izbzlsx = (int)Math.Round(izbzl * 10 / 100 + izbzl);
                    izbzlxx = (int)Math.Round(izbzl - izbzl * 10 / 100);
                    iblsxz = izbzlsx;
                    iblxxz = izbzlxx;
                }
                else
                {
                    izbzlsx = (int)Math.Round(500 + izbzl);
                    izbzlxx = (int)Math.Round(izbzl - 500);
                    iczsxz = izbzlsx;
                    iczxxz = izbzlxx;
                }

            }
            else if (scllx.Contains("轻型") || scllx.Contains("微型"))
            {
                szbzlxz = "±10％或±200kg";

                if (izbzl * 10 / 100 >= 200)
                {
                    izbzlsx = (int)Math.Round(izbzl * 10 / 100 + izbzl);
                    izbzlxx = (int)Math.Round(izbzl - izbzl * 10 / 100);
                    iblsxz = izbzlsx;
                    iblxxz = izbzlxx;
                }
                else
                {
                    izbzlsx = (int)Math.Round(200 + izbzl);
                    izbzlxx = (int)Math.Round(izbzl - 200);
                    iczsxz = izbzlsx;
                    iczxxz = izbzlxx;
                }
                if (izbzlsx > 4500)
                {
                    izbzlsx = 4500;
                    iczsxz = izbzlsx;
                    iblsxz = izbzlsx;
                }
            }
            else
            {
                szbzlxz = "±10％或±200kg";

                if (izbzl * 10 / 100 >= 200)
                {
                    izbzlsx = (int)Math.Round(izbzl * 10 / 100 + izbzl);
                    izbzlxx = (int)Math.Round(izbzl - izbzl * 10 / 100);
                    iblsxz = izbzlsx;
                    iblxxz = izbzlxx;
                }
                else
                {
                    izbzlsx = (int)Math.Round(200 + izbzl);
                    izbzlxx = (int)Math.Round(izbzl - 200);
                    iczsxz = izbzlsx;
                    iczxxz = izbzlxx;
                }
                if (izbzlsx > 4500)
                {
                    izbzlsx = 4500;
                    iczsxz = izbzlsx;
                    iblsxz = izbzlsx;
                }
            }



            //izbzlpanding = 1;
            //zbzlpd = "1";
        }

        public void pandingzbzl(int izbzljcz, string szbzlxz, int iblsxz, int iblxxz, int iczsxz, int iczxxz, int izbzlsx, int izbzlxx,int izbkcbs=1)
        {
            double izbzl = flowbass.iCLZBZL;
            string scllx = flowbass.sCllx;
            string revert;
            //int iczsxz = 0;//差值上限值
            //int iczxxz = 0;//差值下限值
            int ipdbl = 0;//比例判定
            int ipdcz = 0;//差值判定

            //增加进行分项目比例判定和差值判定--
            if (izbzljcz > iblsxz || izbzljcz < iblxxz)
            {
                ipdbl = 2;
            }
            else
            {
                ipdbl = 1;
            }
            if (scllx.Contains("摩托"))
            {
                ipdbl = 1;
            }
            if (izbzljcz > iczsxz || izbzljcz < iczxxz)
            {
                ipdcz = 2;
            }
            else
            {
                ipdcz = 1;
            }
            //增加进行分项目比例判定和差值判定
            string szbzlpd;
            int izbzlpanding;
            if (izbzljcz > izbzlsx || izbzljcz < izbzlxx)
            {
                szbzlpd = "不合格";
                izbzlpanding = 2;
                if (!bzbzlpd)
                {
                    flowbass.getbuhge(flowbass.sDetectID, "Z1");
                }
               
            }
            else
            {
                izbzlpanding = 1;
                szbzlpd = "合格";
            }
            if (bzbzlpd)
            {
                izbzlpanding = 3;
                szbzlpd = "不判定";
            }
            string bz = "";
            bz = string.Format("差值:{0}kg、比例:{1}％",
                               (izbzljcz - izbzl) > 0 ? ("+" + (izbzljcz - izbzl).ToString()) : (izbzljcz - izbzl).ToString(),
                               ((izbzljcz - izbzl) / izbzl * 100) > 0 ? ("+" + ((izbzljcz - izbzl) / izbzl * 100).ToString("0.0")) : ((izbzljcz - izbzl) / izbzl * 100).ToString("0.0"));

            string szbzljieguo = "";
            if (bfbxs)
            {
                szbzljieguo = string.Format("{4}(kg),{0},{1},{2},{3}", izbzljcz, szbzlxz, szbzlpd, bz, izbkcbs==1?"空车质量":"整备质量");
            }
            else
            {
                szbzljieguo = string.Format("{5}(kg),{0},{1}～{2},{3},{4}", izbzljcz, izbzlxx, izbzlsx, szbzlpd, bz, izbkcbs == 1 ? "空车质量" : "整备质量");
            }

            if (flowbass.wbzl > 1)//有尾板质量时候增加备注
            {
                szbzljieguo = szbzljieguo + string.Format(",尾板质量为:{0}", flowbass.wbzl);
            }


            flowbass.AddData(flowbass.sDetectID, "DET_ZBZLJL", "Detects_SJA", izbzlpanding);
            flowbass.AddData(flowbass.sDetectID, "PD_ZBZL", "PD_AJ", szbzljieguo);
            flowbass.AddData(flowbass.sDetectID, "DET_ZBLZ_BLPD", "Detects_SJA", ipdbl);
            flowbass.AddData(flowbass.sDetectID, "DET_ZBLZ_CZPD", "Detects_SJA", ipdcz);
            flowbass.LedWrite(izbkcbs == 1 ? "空车质量" : "整备质量", izbzljcz.ToString(), 1);

            Thread.Sleep(xssj);

            flowbass.LedWrite((izbkcbs == 1 ? "空车质量" : "整备质量") + "结果", szbzlpd, 1);
            Thread.Sleep(xssj);
            xianshineirong = szbzlpd;
            xianshi();
            model.安检联网.modelZ1 objbass = new model.安检联网.modelZ1();
            objbass.zbzl = izbzljcz.ToString();
            objbass.zbzlpd = izbzlpanding.ToString();
            if (flowbass.bnj)
            {
                objbass.clzl = (izbzljcz + flowbass.wbzl).ToString();
                objbass.wbzl = (flowbass.wbzl > 1) ? flowbass.wbzl.ToString() : "";
                objbass.qtbjzl = "";
                objbass.qtbjsm = "";
                objbass.bzzczbzl = izbzl.ToString("0");
                objbass.zczbzlbfb = Math.Round((izbzljcz - izbzl) / izbzl * 100, 1).ToString();
                revert = flowbass.objajlwbass.write(flowbass.objcarclass, objbass);

                if (!(revert.Length > 0 && revert[0].ToString() == "1"))
                {
                    flowbass.LedWrite("联网失败", revert, 1); Thread.Sleep(2000);
                }
            }

        }
    
    }
}
